<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // function myPromise(callBack) {
        //     let seft = this;
        //     selft.status = "pending";
        //     selft.value = undefined;
        //     selft.reson = undefined;

        //     function resolve(value) {
        //         if (self.status === 'pending') {
        //             selft.value = value;
        //             selft.status = "resolved";
        //         }
        //     }

        //     function reject(reason) {
        //         if (self.status === 'pending') {
        //             selft.reason = reason;
        //             selft.status = "rejected";
        //         }
        //     }
        //     try {
        //         callBack(resolve, reject);
        //     } catch (error) {
        //         reject(error);
        //     }
        // }

        // myPromise.then = function(resolve, reject) {
        //     let self = this;
        //     switch (self.status) {
        //         case "resolved":
        //             resolve(self.value);
        //             break;
        //         case "rejected":
        //             reject(self.reason);
        //             break;
        //         default:
        //             reject(self.reason);
        //     }
        // }

        // var p = new myPromise(function(resolve, reject) {
        //     resolve(1)
        // });
        // p.then(function(x) {
        //     console.log(x) // 1
        // })

        class MyPromise {
            status = "pending";
            value = null;
            reson = null;
            onFulfilled = [];
            onRejected = [];
            constructor(callBack) {
                try {
                    callBack(this.resolve.bind(this), this.reject.bind(this))
                } catch (e) {
                    this.reject(e);
                }
            }

            resolve(value) {
                if (this.status === "pending") {
                    this.value = value;
                    this.status = "resolved";
                    this.onFulfilled.map(item => item(value));
                }
            }

            reject(reason) {
                if (this.status === "pending") {
                    this.reason = reason;
                    this.status = "reject";
                    this.onRejected.map(item => item(value));
                }
            }

            then(resolve, reject) {
                switch (this.status) {
                    case "resolved":
                        resolve && resolve(this.value);
                        break;
                    case "rejected":
                        reject && reject(this.reason);
                        break;
                    default:
                        resolve && this.onFulfilled.push(resolve)
                        reject && this.onRejected.push(reject)
                }
            }
        }

        const test = new MyPromise(function(resolve, reject) {
            setTimeout(() => {
                resolve(1);
            }, 1000);
        });
        test.then((e) => {
            console.log(e)
        }, e => {
            console.log(e)
        });

        function gcdOfStrings(str1, str2) {
            if (!(str1.concat(str2) === str2.concat(str1))) {
                return "";
            }
            return str1.substring(0, gcd(str1.length(), str2.length()));
        }

        function gcd(a, b) {
            const remainder = a % b;
            while (remainder != 0) {
                a = b;
                b = remainder;
                remainder = a % b;
            }
            return b;
        }
    </script>
</body>

</html>